﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Data;
using System.Data.OleDb;

namespace Marbles.Data
{
    public class OleDbDataSourceProvider : BaseDataSource, IDataSource
    {
        // Fields
        private OleDbConnection _dbc = null;

        // Methods
        public OleDbDataSourceProvider(string ConnectionString)
        {
            this._dbc = new OleDbConnection(ConnectionString);
        }

        public void Close()
        {
            if (this._dbc.State != ConnectionState.Closed)
            {
                this._dbc.Close();
            }
        }

        private OleDbCommand ConvertToSQLCommand(DataSourceCommand command)
        {
            string query = command.CommandText;
            OleDbCommand cmd = new OleDbCommand(query, this._dbc);
            foreach (string s in command.Parameters.Keys)
            {
                if (command.Parameters[s].GetType() == typeof(byte[]))
                {
                    OleDbParameter parm = new OleDbParameter(s, SqlDbType.Image);
                    parm.Value = command.Parameters[s];
                    cmd.Parameters.Add(parm);
                }
                else
                {
                    if (command.Parameters[s] == null)
                    {
                        cmd.Parameters.Add(new OleDbParameter(s, DBNull.Value));
                    }
                    else
                    {
                        cmd.Parameters.Add(new OleDbParameter(s, command.Parameters[s]));
                    }
                }
            }
            return cmd;
        }

        public int ExecuteCommand(DataSourceCommand Command)
        {
            OleDbCommand cmd = this.ConvertToSQLCommand(Command);
            if (!this.IsConnected())
            {
                this.Open();
            }
            int value = cmd.ExecuteNonQuery();
            this.Close();
            return value;
        }

        public DataSet FetchDataSet(DataSourceCommand Command)
        {
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter(this.ConvertToSQLCommand(Command));
            if (!this.IsConnected())
            {
                this.Open();
            }
            da.Fill(ds);
            this.Close();
            return ds;
        }

        public DataTable FetchDataTable(DataSourceCommand Command)
        {
            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter(this.ConvertToSQLCommand(Command));
            if (!this.IsConnected())
            {
                this.Open();
            }
            da.Fill(dt);
            this.Close();
            return dt;
        }

        public IDataReader FetchReader(DataSourceCommand Command)
        {
            return this.ConvertToSQLCommand(Command).ExecuteReader();
        }

        public bool IsConnected()
        {
            return (this._dbc.State == ConnectionState.Open);
        }

        public void Open()
        {
            if ((this._dbc.State != ConnectionState.Connecting) & (this._dbc.State != ConnectionState.Open))
            {
                this._dbc.Open();
            }
        }

        // Properties
        public string Provider
        {
            get
            {
                return "OleDbClient";
            }
        }

        public virtual string[] ListDatabases()
        {
            return new string[] { };
        }

        public virtual DataSourceTable[] ListTables(string Database)
        {
            List<DataSourceTable> tbl = new List<DataSourceTable>();
            return tbl.ToArray();
        }

        public virtual DataSourceTableDefinition GetTableDefinition(DataSourceTable Table)
        {
            DataSourceTableDefinition tbl = new DataSourceTableDefinition(Table);
            return tbl;
        }
    }
}
